$(function(){

	//按钮一
    var type,year,month,date,textName,searchTimmer,fuText,binText;
    var searchb = function()
    {
    	clearTimeout(searchTimmer);
    	searchTimmer = setTimeout(function(){
	    	type = $('.dataval:visible').data('type');
	    	if(type == 0)
		    {
		    	year = $('.year').val();
		    	url = "/modules/operatingStatistics/profit.html?type=" + type + "&year=" + year;
		    	ajaxUrl2(url,type);
		    }
		    else if(type == 1)
		    {
		    	year = $('.year2').val();
				url = "/modules/operatingStatistics/loss.html?type=" + type + "&year=" + year;
		    	ajaxUrl2(url,type);
		    }
		    else if(type == 2)
		    {
		    	year = $('.year3').val();
		   		url = "/modules/operatingStatistics/profitAndLossAnalysis.html?type=" + type + "&year=" + year;
		   		ajaxUrl2(url,type);
		    }				
    	}, 100);
    	
    }
    
	var ajaxUrl2 = function (url,type){
		$.ajax({
			url:url + "&randomTime=" + (new Date()).getTime(),
			type:"get",
			dataType:"json",
			cache:false,
			success:function(data){
				var option;
				if(type == 0)//type0饼图+折线图
				{
					var sums = 0;
					for(var i=0;i<data.totalFee.length;i++)
					{
						sums = sums + data.totalFee[i];
						if(sums == 0)
						{
							fuText = '暂无数据';
							binText = '暂无数据';
						}
						else
						{
							fuText = '';
							binText = '*其中借款手续费不包含债权转让手续费'
						}
					}
					var dataManageFee = [],dataLegend = [];
					for (var i in data) {
						if(data[i] != 0)
						{
							switch(i)
							{
								case 'borrowManageFee':
								dataLegend.push('借款手续费');
								dataManageFee.push({value:data.borrowManageFee,name:'借款手续费'});
								break;
								case 'interestManageFee':
								dataLegend.push('利息管理费');
								dataManageFee.push({value:data.interestManageFee,name:'利息管理费'});
								break;
								/*case 'bondManageFee':
								dataLegend.push('债权转让手续费');
								dataManageFee.push({value:data.bondManageFee,name:'债权转让手续费'});
								break;*/
							}
						}
						else
						{
							switch(i)
							{
								case 'borrowManageFee':
								dataLegend.push('借款手续费');
								dataManageFee.push({name:'借款手续费'});
								break;
								case 'interestManageFee':
								dataLegend.push('利息管理费');
								dataManageFee.push({name:'利息管理费'});
								break;
								/*case 'bondManageFee':
								dataLegend.push('债权转让手续费');
								dataManageFee.push({name:'债权转让手续费'});
								break;*/
							}
						}
					};
					option1 = {
						title : {
						    text: '平台收入',
						    x : 'center',
					        y : 'top',
					        subtext:fuText,
			            	textStyle:{
									fontSize: 24, 
									fontWeight: 'bolder'	
			            	}
						},
					    tooltip : {
					        trigger: 'axis'
					    },
					    legend: {
					    	x:'center',
					    	y:'bottom',
					        data:['收入金额']
					    },
					    xAxis : [
					        {
					            type : 'category',
					            boundaryGap : false,
					            data : ['一月','二月','三月','四月','五月','六月','七月','八月','九月','十月','十一月','十二月']
					        }
					    ],
					    yAxis : [
					        {
					            type : 'value',
					            axisLabel : {
					                formatter: '{value} 元'
					            }
					        }
					    ],
					    series : [
					        {
					            name:'收入金额',
					            type:'line',
					            data:data.totalFee,
					            markPoint : {
					                data : [
					                    {type : 'max', name: '最大值'},
					                    {type : 'min', name: '最小值'}
					                ]
					            },
					            markLine : {
					                data : [
					                    {type : 'average', name: '平均值'}
					                ]
					            }
					        }
					    ]
					}
					option2 = {
						title : {
						    text: '收入项分布',
						    x : 'center',
						    subtext:binText,
					        y : 'top',
			            	textStyle:{
									fontSize: 24, 
									fontWeight: 'bolder'	
			            	}
						},
						legend: {
					        orient : 'vertical',
					        x : '80%',
					        y : 'center',
					        data:dataLegend
					    },
					    calculable : true,
						tooltip : {
						    show: true,
						    formatter: "{a} <br/>{b} : {c} ({d}%)"
						},
						series : [
						    {
						        name:'收入项分布',
						        type:'pie',
						        radius : '50%',
						        center: ['50%', '65%'],
						        data:dataManageFee
						    }
						]
					}
					echarts.init(document.getElementById('investTablea')).setOption(option1); 
			        echarts.init(document.getElementById('investTableb')).setOption(option2); 					                    
				}
				else if(type == 1)//type1 饼图+折线图
				{
					var sums = 0;
					for(var i=0;i<data.totalFee.length;i++)
					{
						sums = sums + data.totalFee[i];
						if(sums == 0)
						{
							fuText = '暂无数据';
							binText = '暂无数据';
						}
						else
						{
							fuText = '';
							binText = '*其中运营支出包含红包活动运营支出、提现手续费、平台送出vip奖励金额、积分抽奖送出现金、平台送出体验金所支付利息、体验券投标所支付利息、加息券所支付利息、平台垫付充值手续费等'
						}
					}
					var dataManageFee = [],dataLegend = [];
					for (var i in data) {
						if(data[i] != 0)
						{
							switch(i)
							{
								case 'userRedPacketFee':
									dataLegend.push('红包活动运营支出');
									dataManageFee.push({value:data.userRedPacketFee,name:'红包活动运营支出'});
									break;
								case 'webAdvanCashFees':
									dataLegend.push('平台垫付提现手续费');
									dataManageFee.push({value:data.webAdvanCashFees,name:'平台垫付提现手续费'});
									break;
								case 'vipInterestFee':
									dataLegend.push('平台送出vip奖励金额');
									dataManageFee.push({value:data.vipInterestFee,name:'平台送出vip奖励金额'});
									break;
								case 'scoreLotteryMoney':
									dataLegend.push('积分抽奖送出现金');
									dataManageFee.push({value:data.scoreLotteryMoney,name:'积分抽奖送出现金'});
									break;
								case 'upRateCollection':
									dataLegend.push('加息券所支付利息');
									dataManageFee.push({value:data.upRateCollection,name:'加息券所支付利息'});
									break;
								case 'webAdvanRechargeFee':
									dataLegend.push('平台垫付充值手续费');
									dataManageFee.push({value:data.webAdvanRechargeFee,name:'平台垫付充值手续费'});
									break;
								case 'experienceMoney':
									dataLegend.push('平台垫付投资体验金');
									dataManageFee.push({value:data.experienceMoney,name:'平台垫付投资体验金'});
									break;
							}
						}
						else
						{
							switch(i)
							{
								case 'userRedPacketFee':
									dataLegend.push('红包活动运营支出');
									dataManageFee.push({name:'红包活动运营支出'});
									break;
								case 'webAdvanCashFees':
									dataLegend.push('提现手续费');
									dataManageFee.push({name:'提现手续费'});
									break;
								case 'vipInterestFee':
									dataLegend.push('平台送出vip奖励金额');
									dataManageFee.push({name:'平台送出vip奖励金额'});
									break;
								case 'scoreLotteryMoney':
									dataLegend.push('积分抽奖送出现金');
									dataManageFee.push({name:'积分抽奖送出现金'});
									break;
								case 'upRateCollection':
									dataLegend.push('加息券所支付利息');
									dataManageFee.push({name:'加息券所支付利息'});
									break;
								case 'webAdvanRechargeFee':
									dataLegend.push('平台垫付充值手续费');
									dataManageFee.push({name:'平台垫付充值手续费'});
									break;
								case 'experienceMoney':
									dataLegend.push('平台垫付投资体验金');
									dataManageFee.push({name:'平台垫付投资体验金'});
									break;
							}
						}
					};
					option3 = {
						title : {
						    text: '平台支出',
						    x : 'center',
					        y : 'top',
			            	textStyle:{
									fontSize: 24, 
									fontWeight: 'bolder'	
			            	},
			            	subtext:fuText
						},
					    tooltip : {
					        trigger: 'axis'
					    },
					    legend: {
					    	x:'center',
					    	y:'bottom',
					        data:['支出金额']
					    },
					    xAxis : [
					        {
					            type : 'category',
					            boundaryGap : false,
					            data : ['一月','二月','三月','四月','五月','六月','七月','八月','九月','十月','十一月','十二月']
					        }
					    ],
					    yAxis : [
					        {
					            type : 'value',
					            axisLabel : {
					                formatter: '{value} 元'
					            }
					        }
					    ],
					    series : [
					        {
					            name:'支出金额',
					            type:'line',
					            data:data.totalFee,
					            markPoint : {
					                data : [
					                    {type : 'max', name: '最大值'},
					                    {type : 'min', name: '最小值'}
					                ]
					            },
					            markLine : {
					                data : [
					                    {type : 'average', name: '平均值'}
					                ]
					            }
					        }
					    ]
					}
					option4 = {
						title : {
						    text: '支出项分布',
						    x : 'center',
					        y : 'top',
			            	textStyle:{
									fontSize: 24, 
									fontWeight: 'bolder'	
			            	},
			            	subtext: binText
						},
						legend: {
					        orient : 'vertical',
					        x : '80%',
					        y : 'center',
					        data:dataLegend
					    },
						tooltip : {
						    show: true,
						    formatter: "{a} <br/>{b} : {c} ({d}%)"
						},
						calculable : true,
						series : [
						    {
						        name:'支出项分布',
						        type:'pie',
						        radius : '50%',
						        center: ['50%', '65%'],
						        data:dataManageFee
						    }
						]
					}
					echarts.init(document.getElementById('investTablec')).setOption(option3); 
			        echarts.init(document.getElementById('investTabled')).setOption(option4); 					                    
				}
				else if(type == 2)//type2 柱状图
				{
					var sumsA = 0,sumsB = 0;
					for (var i = 0; i < data.totalLossFee.length; i++) {
						data.totalLossFee[i] = 0-data.totalLossFee[i];
						sumsA = sumsA+data.totalLossFee[i];
					};
					for (var i = 0; i < data.totalProfitFee.length; i++) {
						sumsB = sumsB+data.totalProfitFee[i];
					};
					if(sumsA == 0 && sumsB == 0)
					{
						fuText = '暂无数据';
					}
					else
					{
						fuText = '';
					}
					option = {
						title : {
						    text: '平台盈亏概况',
						    x : 'center',
					        y : 'top',
			            	textStyle:{
									fontSize: 24, 
									fontWeight: 'bolder'	
			            	},
			            	subtext:fuText
						},
			        	tooltip : {
					        trigger: 'axis',
					        axisPointer : {            // 坐标轴指示器，坐标轴触发有效
					            type : 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
					        }
					    },
					    legend: {
					    	y:'bottom',
					        data:['收入', '支出']
					    },
					    xAxis : [
					        {
					            type : 'category',
					            axisTick : {show: false},
					            data : ['一月','二月','三月','四月','五月','六月','七月','八月','九月','十月','十一月','十二月']
					        }
					    ],
					    yAxis : [
					        {	
					        	name:'盈亏总额（元）',
					            type : 'value'
					        }
					    ],
					    series : [
					        {
					            name:'收入',
					            type:'bar',
					            stack: '总量',
					            barWidth : 5,
					            itemStyle: {normal: {
					                label : {show: true}
					            }},
					            data:data.totalProfitFee
					        },
					        {
					            name:'支出',
					            type:'bar',
					            stack: '总量',
					            itemStyle: {normal: {
					                label : {show: true, position: 'left'}
					            }},
					            data:data.totalLossFee
					        }
					    ],
					    dataRange: {
					    	padding: 100,
					    }
			        };
					echarts.init(document.getElementById('investTablee')).setOption(option); 
				}
			}
		});
	};

	var changBtn = $('.investorsTabs .tabs-header ul li span');
	changBtn.click(function(){
		searchb();//初始化加载
	});
	searchb();
    $(".year,.year2,.year3").change(function(){
    	searchb();
    });

});